/**
 * File    : StartupListener.java
 * Created : 2008-9-5
 * By      : LuHuiguo
 */
package com.luhuiguo.sso.tutorial.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.luhuiguo.sso.tutorial.Setup;

/**
 * @author LuHuiguo
 * @version $Id: StartupListener.java 2 2008-10-04 13:04:21Z LuHuiguo $
 * 
 */
public class StartupListener implements ServletContextListener {
	private static final Logger logger = LoggerFactory
			.getLogger(StartupListener.class);

	public void contextInitialized(ServletContextEvent event) {

		logger.debug("Initializing context...");

		ServletContext context = event.getServletContext();

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);

		Setup setup = (Setup) ctx.getBean("setup");
		setup.run();

		/*
		 * String[] beans = ctx.getBeanDefinitionNames(); for (String bean :
		 * beans) { log.debug(bean); }
		 */

		setupContext(context);
	}

	/**
	 * This method uses the LookupManager to lookup available roles from the
	 * data layer.
	 * 
	 * @param context
	 *            The servlet context
	 */
	public static void setupContext(ServletContext context) {
	}

	/**
	 * Shutdown servlet context (currently a no-op method).
	 * 
	 * @param servletContextEvent
	 *            The servlet context event
	 */
	public void contextDestroyed(ServletContextEvent servletContextEvent) {

		// LogFactory.release(Thread.currentThread().getContextClassLoader());
		// Commented out the above call to avoid warning when SLF4J in
		// classpath.
		// WARN: The method class
		// org.apache.commons.logging.impl.SLF4JLogFactory#release() was
		// invoked.
		// WARN: Please see http://www.slf4j.org/codes.html for an explanation.
	}
}
